package com.zxl.listener;

import com.rabbitmq.client.Channel;
import com.zxl.constants.ESConstants;
import com.zxl.service.ESService;
import com.zxl.vo.Goods;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


/**
 * @packageName com.zxl.listener
 * @ClassName
 * @Author zxl
 * @Description //TODO
 * @date
 **/
@Component
public class MQListener {

    @Autowired
    public ESService esService;

    @RabbitListener(queues = ESConstants.GOODS_QUEUE)
    public void goodsOperate(Goods goods, Channel channel , Message message) throws Exception {
        try {
            System.out.println(goods);
            String routingKey = message.getMessageProperties().getReceivedRoutingKey();
            switch (routingKey){
                case "goods.add":
                    esService.add(goods);
                    break;
            }
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            e.printStackTrace();
            channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,true);
        }

    }
}
